#!/usr/bin/python
# -*- coding: utf-8 -*-
#Under the terms of the General Public License v.3
#For more information see LICENSE.txt

#from Robot import *
#from Human import *
#from Item import *
#from AccessPoint import *
#from Obstacle import *

import pygame
from pygame.locals import K_LEFT, K_RIGHT, K_UP, K_DOWN, QUIT, KEYDOWN

import Player

WIDTH = 1024#320
HEIGTH = 768#240

class Map(object):

	"""
	 This is the map of the game, please document all your updates.

	:version:
	:author:
	"""

	""" ATTRIBUTES

	 Width (X)

	sizeX  (private)

	 Higth (Y)

	sizeY  (private)


	robot  (private)


	humans  (private)


	itms  (private)


	obstacles  (private)


	accesPoint  (private)

	"""

	def __init__(self):
		#This initiate the matrix of the map
		self.matrix = self.loadMatris ('./data/maps/Map.txt')
		self.blocks = self.loadBlockImage()
		self.backgrown = self.newBack()
		self.drawBack()
		self.camera_x = 0
		self.camera_y = 0
		self.destination_camera_x = 0 #200
		self.destination_camera_y = 0 #100

	def loadMatris(self, path):
		#this funtion return de content of Map.txt
		fileMap = file(path,'rt')
		content = fileMap.readlines()
		fileMap.close()
		return content

	def loadBlockImage(self):
		#this funtion return a list of all block image of de map
		blocks = []
		for n in range(3):
			blocks.append(pygame.image.load('./data/image/%d.png' %n).convert_alpha())
		return blocks

	def newBack(self):
		#this funtion return a surface size
		arrows = len(self.matrix)
		columns = ( len(self.matrix[0]) - 1 )
		width = columns * 48
		heigth = arrows * 48
		return pygame.Surface( (width, heigth) ).convert()

	def drawBack(self):
		numberOfArrows = len(self.matrix)
		numberOfColumns = (len(self.matrix[0]) - 1)

		print "\nThis Map is Load in matrix", ' ',numberOfArrows,'x',numberOfColumns
		for f in range(numberOfArrows):
			print
			for c in range(numberOfColumns):
				print self.matrix[f][c],

		self.backgrown.fill((150, 150, 255))
		for arrow in range(numberOfArrows):
			for column in range(numberOfColumns):
				index = int(self.matrix[arrow][column])
				pos_x = column * 48# + 48
				pos_y = arrow * 48# + 48
				self.backgrown.blit(self.blocks[index], (pos_x, pos_y))

	def show(self, screen):
		origin = (self.camera_x, self.camera_y, WIDTH, HEIGTH)
		destination = (0, 0, WIDTH, HEIGTH)
		screen.blit(self.backgrown, destination, origin)

	def move(self, dx, dy):
		self.destination_camera_x += dx
		self.destination_camera_y += dy
		self.limits()

	def update(self):
		#self.camera_x += (self.destination_camera_x - self.camera_x) / 10.0
		#self.camera_y += (self.destination_camera_y - self.camera_y) / 10.0
		pass

	def limits(self):
		if self.destination_camera_x < 0:
			self.destination_camera_x = 0

		if self.destination_camera_y < 0:
			self.destination_camera_y = 0

		if self.destination_camera_x + WIDTH > self.backgrown.get_width():
			self.destination_camera_x = self.backgrown.get_width() - WIDTH

		if self.destination_camera_y + HEIGTH > self.backgrown.get_height():
			self.destination_camera_y = self.backgrown.get_height() - HEIGTH

	def lookAt(self, rect):
		self.destination_camera_x = rect.x - WIDTH / 2
		self.destination_camera_y = rect.y - HEIGTH / 2
		self.limits()
	"""
	def cantStep(self, x, y):
		arrow = y / 48
		column = x / 48
		try:
			place = int(self.matrix[arrow][column])
		except:
			return False
		if place in [0, 1]:
			return True #esto está concptualmente mal, si PUEDE caminar debe devolver False
		return False
	"""
	def cantStep(self,x,y):
		robot_box=[ y-24, y+24, x-24, x+24]
		arrow = y / 48
		column = x / 48
		print "arrow %s" %arrow
		print "colum %s" %column
		#left_limit = column * 48
		#rigth_limit = left_limit + 48
		#up_limit = arrow * 48
		#down_limit = up_limit + 48
		limits = [column * 48,column * 48 + 48, arrow * 48, arrow * 48 + 48]
		for i in range(4):
			print "robot_box[%s] = %s" %(i,robot_box[i])
			print "limits[%s] = %s" %(i,limits[i])
		try:
			place = int(self.matrix[arrow][column])
		except:
			return False
		if place in [2]:
			if (robot_box[0] <= limits[0] or \
				robot_box[1] >= limits[1]) and \
				(robot_box[2] <= limits[2] or \
				robot_box[3] >= limits[3]):
				print "No está dentro de los límites"
				return False
		return True
			
	#def getRobot(self):
		#"""

		#@return Robot :
		#@author
		#"""
		#pass

	#def setRobot(self):
		#"""

		#@return  :
		#@author
		#"""
		#pass

	#def getHuman(self):
		#"""

		#@return Human :
		#@author
		#"""
		#pass

	#def setHuman(self):
		#"""

		#@return  :
		#@author
		#"""
		#pass

	#def getItem(self):
		#"""

		#@return Item :
		#@author
		#"""
		#pass

	#def setItem(self):
		#"""

		#@return  :
		#@author
		#"""
		#pass

	#def getAccessPoint(self):
		#"""

		#@return AccessPoint :
		#@author
		#"""
		#pass

	#def setAccessPoint(self):
		"""

		@return  :
		@author
		"""
		pass


if __name__ == '__main__':

	def main():
		screen = pygame.display.set_mode((WITH, HEIGTH))
		pygame.display.set_caption("Map Module Test - Robot Game")

		mapa = Map() #no utilizar la palabra reservada "map"
		player = Player(mapa)#no utilizar la palabra reservada "map"
		exit_ = False

		while not exit_:

			for e in pygame.event.get():
				if e.type == QUIT:
					exit_ = True
				elif e.type == KEYDOWN and e.unicode == 'q':
					exit_ = True

			player.update()
			mapa.update()
			mapa.show(screen)
			pygame.display.flip()
			pygame.time.wait(10)

		pygame.display.init()

	main()
